package com.jy.appmarket.utils;

import java.util.Set;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

public class Settings {

	private static final String NOTIFICATION = "notification";
	private static final String COUNT = "count";
	private static final String IS_OPEN = "is_open";
	private static final String LAST_NOTIFICATION = "last_notification";
	private static final String BADGE_APPS = "badge_apps";

	public static int readCounts(Context context) {
		SharedPreferences sp = context.getSharedPreferences(NOTIFICATION, Context.MODE_PRIVATE);
		return sp.getInt(COUNT, -1);
	}

	public static void writeCounts(Context context, int count) {
		SharedPreferences sp = context.getSharedPreferences(NOTIFICATION, Context.MODE_PRIVATE);
		Editor edit = sp.edit();
		edit.putInt(COUNT, count);
		edit.commit();
	}

	public static boolean ReadIsOpen(Context context) {
		SharedPreferences sp = context.getSharedPreferences(NOTIFICATION, Context.MODE_PRIVATE);
		return sp.getBoolean(IS_OPEN, false);
	}

	public static void WriteIsOpen(Context context, boolean state) {
		SharedPreferences sp = context.getSharedPreferences(NOTIFICATION, Context.MODE_PRIVATE);
		Editor edit = sp.edit();
		edit.putBoolean(IS_OPEN, state);
		edit.commit();
	}

	public static int ReadLastNotification(Context context) {
		SharedPreferences sp = context.getSharedPreferences(NOTIFICATION, Context.MODE_PRIVATE);
		return sp.getInt(LAST_NOTIFICATION, 0);
	}

	public static void WriteLastNotification(Context context, int lastTimeNotify) {
		SharedPreferences sp = context.getSharedPreferences(NOTIFICATION, Context.MODE_PRIVATE);
		Editor edit = sp.edit();
		edit.putInt(LAST_NOTIFICATION, lastTimeNotify);
		edit.commit();
	}

	public static Set<String> getBadgePkgs(Context context) {
		SharedPreferences sp = context.getSharedPreferences(NOTIFICATION, Context.MODE_PRIVATE);
		return sp.getStringSet(BADGE_APPS, null);
	}

	public static void setBadgePkgs(Context context, Set<String> apps) {
		SharedPreferences sp = context.getSharedPreferences(NOTIFICATION, Context.MODE_PRIVATE);
		Editor edit = sp.edit();
		if (apps == null || apps.size() == 0) {
			edit.remove(BADGE_APPS);
		} else {
			edit.putStringSet(BADGE_APPS, apps);
		}
		edit.commit();
	}
}
